// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <lib/mmio/mmio.h>

#include <fbl/algorithm.h>
#include <zxtest/zxtest.h>

namespace ddk_mock_test {

TEST(MockMmioReg, CopyFrom) {
  ddk_mock::MockMmioReg reg_array_1[0x100];
  ddk_mock::MockMmioReg reg_array_2[0x100];

  ddk_mock::MockMmioRegRegion reg_region_1(reg_array_1, sizeof(uint32_t),
                                           fbl::count_of(reg_array_1));
  ddk_mock::MockMmioRegRegion reg_region_2(reg_array_2, sizeof(uint32_t),
                                           fbl::count_of(reg_array_2));

  ddk::MmioBuffer dut_1(reg_region_1.GetMmioBuffer());
  ddk::MmioBuffer dut_2(reg_region_2.GetMmioBuffer());

  constexpr uint32_t reg_values[] = {0xdb5a95fd, 0xc1c8f880, 0x733c2bed, 0xf74e857c};
  for (size_t i = 0; i < fbl::count_of(reg_values); i++) {
    reg_region_1[0x10 + (i * 4)].ExpectRead(reg_values[i]);
    reg_region_2[0x40 + (i * 4)].ExpectWrite(reg_values[i]);
  }

  dut_2.CopyFrom32(dut_1, 0x10, 0x40, 4);

  ASSERT_NO_FATAL_FAILURES(reg_region_1.VerifyAll());
  ASSERT_NO_FATAL_FAILURES(reg_region_2.VerifyAll());
}

}  // namespace ddk_mock_test
